const userModel = require("../models/user_model");

// 登录   
function doLogin(user, onRetGot, onErrGot) {
    // onErrGot({ msg: "登录失败" });
    userModel.login(
        user,
        (ret) =>
            onRetGot(
                ret ? { ret, msg: "登录成功" ,code:200,authorization:"adsaskdjasf54654k"} : { msg: "用户名或密码错误" ,code:400}
            ),
        (err) => onErrGot({ err, msg: "登录失败" ,code:200})
    );
}

//注册
function doregister(user, onRetGot, onErrGot) {
     userModel.register(
        user,
        (ret) => onRetGot({ ret, msg: "注册成功" ,code:200}),
        (err) => onErrGot({ err, msg: "注册失败" ,code:400})
    );
}


//修改
function doupdate(user, onRetGot, onErrGot) {
     userModel.update(
        user,
        (ret) => onRetGot({ ret, msg: "修改成功" ,code:200}),
        (err) => onErrGot({ err, msg: "修改失败" ,code:400})
    );
}


//根据id查询
function doselect(user, onRetGot, onErrGot) {
     userModel.select(
        user,
        (ret) => onRetGot({ ret, msg: "查询成功" ,code:200}),
        (err) => onErrGot({ err, msg: "查询失败" ,code:400})
    );
}



//删除
function dodelete(user, onRetGot, onErrGot) {
     userModel.deleteuser(
        user,
        (ret) => onRetGot({ ret, msg: "删除成功" ,code:200}),
        (err) => onErrGot({ err, msg: "删除失败" ,code:400})
    );
}


//查询全部
function doall(user, onRetGot, onErrGot) {
     userModel.all(
        user,
        (ret) => onRetGot({ ret, msg: "查询成功" ,code:200}),
        (err) => onErrGot({ err, msg: "查询失败" ,code:400})
    );
}






const userController = {
    doLogin,
    doregister,
    doupdate,
    doselect,
    dodelete,
    doall
};

module.exports = userController;
